package com.itboye.ihomebank.zjtlcb.lnd.livenessdetector.utils;

import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by TLCB on 2018/6/5.
 */
public class OkHttpRequestUtil {
    public String okHttpRequest(String strRequest, String requestURL, String requestCode ,String TAG) throws IOException {
        String reponseStr = "";
        MediaType JSON = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");
        OkHttpClient.Builder mBuilder = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(30,TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS);

        mBuilder.sslSocketFactory(createSSLSocketFactory());
        mBuilder.hostnameVerifier(new TrustAllHostnameVerifier());

        OkHttpClient okHttpClient = mBuilder.build();;
        RequestBody body = RequestBody.create(JSON, strRequest);
        Request request = new Request.Builder()
                .url(requestURL + requestCode)
                .post(body)
                .build();
        Response response = null;
        response = okHttpClient.newCall(request).execute();
        if(response.isSuccessful()){
            reponseStr = response.body().string();
            //Log.e(TAG, requestCode + "请求接收到的报文:" + reponseStr);

            return reponseStr;
        }else{
            reponseStr = "金融云后台未返回";
            return reponseStr;
        }
    }

    //https处理
    private static class TrustAllCerts implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}

        @Override
        public X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}
    }

    private static class TrustAllHostnameVerifier implements HostnameVerifier {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

    private static SSLSocketFactory createSSLSocketFactory() {
        SSLSocketFactory ssfFactory = null;

        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null,  new TrustManager[] { new TrustAllCerts() }, new SecureRandom());

            ssfFactory = sc.getSocketFactory();
        } catch (Exception e) {
        }

        return ssfFactory;
    }
}
